<?php

namespace App\Admin\Controllers;

use App\Admin\Filters\WorshipHymnFilter;
use App\Admin\Models\SystemModel;
use App\Admin\Models\SystemMedia;
use App\Admin\Models\WorshipHymn;
use App\Admin\Requests\WorshipHymnRequest;
use App\Admin\Resources\WorshipHymnResource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;

class WorshipHymnController extends Controller
{
    public function index(WorshipHymnFilter $filter, WorshipHymn $worshipHymn, SystemMedia $systemMedia)
    {
        if ($idStr = request()->input('id')) {
            $order = "FIELD(id, $idStr)";
        } else {
            $order = "updated_at desc";
        }
        $worshipHymns = WorshipHymn::withTrashed()
            ->filter($filter)
			->select(SystemModel::getFields($worshipHymn->getTable()))
			->orderByRaw(DB::raw($order))
            ->paginate();
        foreach ($worshipHymns as $key => $value) {
            if (isset($value['lyric'])) {
                $worshipHymns[$key]['lyric'] = $systemMedia->select(['id','origin_filename','path'])->find($value['lyric']);
            }
            if (isset($value['mp3'])) {
                $worshipHymns[$key]['mp3'] = $systemMedia->select(['id','origin_filename','path'])->find($value['mp3']);
            }
            isset($value['content']) && $worshipHymns[$key]['content'] = $worshipHymn->formaContent($value['content']);
            isset($value['spectrum']) && $worshipHymns[$key]['spectrum'] = $worshipHymn->spectrums($value['spectrum']);
        }
        return $this->ok(WorshipHymnResource::collection($worshipHymns));
    }

    public function create()
    {
        return $this->ok();
    }

    public function store(WorshipHymnRequest $request, WorshipHymn $worshipHymn)
    {
        $inputs = $request->validated();
        $token = md5(json_encode($inputs));
        unset($inputs['token']);
        if (Cache::get($token) == 2) {
            return $this->error('请不要重复提交数据');
        } 

        if (isset($inputs['mp3']['path'])) {
            if ($inputs['duration']=='0.00') {
                $inputs['duration'] = getMp3Duration($inputs['mp3']['path']);
            }
            $inputs['mp3'] = $inputs['mp3']['id'];
        }
        if (isset($inputs['spectrum'])) {
            $inputs['spectrum'] = array_column($inputs['spectrum'], 'id');
        }
        if (isset($inputs['lyric']['path'])) {
            $inputs['lyric'] = $inputs['lyric']['id'];
        }
        Cache::put($token, 1);
        $worshipHymn = $worshipHymn->add($inputs);
        Cache::put($token, 2);
        if (isset($inputs['albums'])) {
            $worshipHymn->albums()->sync($inputs['albums']);
        }
        if (isset($inputs['artists'])) {
            $worshipHymn->artists()->sync($inputs['artists']);
        }
        return $this->created(WorshipHymnResource::make($worshipHymn));
    }

    public function edit(Request $request, WorshipHymn $worshipHymn)
    {
        $worshipHymn->load(['mp3', 'lyric']);
        isset($worshipHymn->spectrum) && $worshipHymn->spectrum = $worshipHymn->spectrums($worshipHymn->spectrum);
        return $this->ok(WorshipHymnResource::make($worshipHymn));
    }

    public function update(WorshipHymnRequest $request, WorshipHymn $worshipHymn)
    {
        $inputs = $request->validated();
        if (isset($inputs['mp3']['path'])) {
            if ($inputs['duration']=='0.00') {
                $inputs['duration'] = getMp3Duration($inputs['mp3']['path']);
            }
            $inputs['mp3'] = $inputs['mp3']['id'];
        }
        if (isset($inputs['spectrum'])) {
            $inputs['spectrum'] = array_column($inputs['spectrum'], 'id');
        }
        if (isset($inputs['lyric']['path'])) {
            $inputs['lyric'] = $inputs['lyric']['id'];
        }
        $worshipHymn->update($inputs);
        if (isset($inputs['albums'])) {
            $worshipHymn->albums()->sync($inputs['albums']);
        }
        if (isset($inputs['artists'])) {
            $worshipHymn->artists()->sync($inputs['artists']);
        }
        return $this->created(WorshipHymnResource::make($worshipHymn));
    }

    public function destroy(WorshipHymn $worshipHymn)
    {
        $worshipHymn->delete();
        return $this->noContent();
    }

	public function show(Request $request, Int $id)
    {
        $worshipHymn = WorshipHymn::withTrashed()
            ->with(['mp3', 'lyric'])
            ->select(SystemModel::getFields(app(WorshipHymn::class)->getTable(), 'detail'))
            ->find($id);
        isset($worshipHymn->content) && $worshipHymn->content = $worshipHymn->formaContent($worshipHymn->content);
        isset($worshipHymn->lyric) && $worshipHymn->lyric = $worshipHymn->formaLyric($worshipHymn->lyric);
        isset($worshipHymn->spectrum) && $worshipHymn->spectrum = $worshipHymn->spectrums($worshipHymn->spectrum);
        return $this->ok(WorshipHymnResource::make($worshipHymn));
    }
}
